/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        ListNode guard(-1);
        guard.next=head;
        head=&guard;
        bool del=false;
        while (head!=NULL)
            if (head->next!=NULL && head->next->next!=NULL && head->next->val==head->next->next->val) {
                head->next->next=head->next->next->next;
                del=true;
            } else
                if (del) {
                    head->next=head->next->next;
                    del=false;
                } else
                    head=head->next;
        return guard.next;
    }
};
